The list of materials and links related to clustering and classification can be found below.
course slides by Emma Kämäräinen
DataCamp exercise
After solving the DataCamp exercise and going through the embedded links, I got a general overview on the topic. In the following sections, I will prepare a report based on the exercise instructions.
Data
In this exercise, I will be using Boston data from MASS package.
library(MASS)
data(Boston)
str(Boston)
## 'data.frame': 506 obs. of 14 variables:
## $ crim : num 0.00632 0.02731 0.02729 0.03237 0.06905 ...
## $ zn : num 18 0 0 0 0 0 12.5 12.5 12.5 12.5 ...
## $ indus : num 2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87 ...
## $ chas : int 0 0 0 0 0 0 0 0 0 0 ...
## $ nox : num 0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524 ...
## $ rm : num 6.58 6.42 7.18 7 7.15 ...
## $ age : num 65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...
## $ dis : num 4.09 4.97 4.97 6.06 6.06 ...
## $ rad : int 1 2 2 3 3 3 5 5 5 5 ...
## $ tax : num 296 242 242 222 222 222 311 311 311 311 ...
## $ ptratio: num 15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...
## $ black : num 397 397 393 395 397 ...
## $ lstat : num 4.98 9.14 4.03 2.94 5.33 ...
## $ medv : num 24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...
dim(Boston)
## [1] 506 14
The Boston data was collected to study the housing values in the suburbs of Boston. The table contains 506 observations for 14 different variables. The descriptions for each of the 14 variables are listed below.
| Variables | Description |
|---|---|
| crim | per capita crime rate by town. |
| zn | proportion of residential land zoned for lots over 25,000 sq.ft. |
| indus | proportion of non-retail business acres per town. |
| chas | Charles River dummy variable (= 1 if tract bounds river; 0 otherwise). |
| nox | nitrogen oxides concentration (parts per 10 million). |
| rm | average number of rooms per dwelling. |
| age | proportion of owner-occupied units built prior to 1940. |
| dis | weighted mean of distances to five Boston employment centres. |
| rad | index of accessibility to radial highways. |
| tax | full-value property-tax rate per $10,000. |
| ptratio | pupil-teacher ratio by town. |
| black | 1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town. |
| lstat | lower status of the population (percent). |
| medv | median value of owner-occupied homes in $1000s. |
Data Summary
Now, let’s look at the summary of the boston data in the form of table (instead of default layout) using pandoc.table function of pander package.
library(pander)
pandoc.table(summary(Boston), caption = "Summary of Boston data", split.table = 120)
##
## -----------------------------------------------------------------------------------------------------------------------
## crim zn indus chas nox rm age
## ------------------ ---------------- --------------- ----------------- ---------------- --------------- ----------------
## Min. : 0.00632 Min. : 0.00 Min. : 0.46 Min. :0.00000 Min. :0.3850 Min. :3.561 Min. : 2.90
##
## 1st Qu.: 0.08204 1st Qu.: 0.00 1st Qu.: 5.19 1st Qu.:0.00000 1st Qu.:0.4490 1st Qu.:5.886 1st Qu.: 45.02
##
## Median : 0.25651 Median : 0.00 Median : 9.69 Median :0.00000 Median :0.5380 Median :6.208 Median : 77.50
##
## Mean : 3.61352 Mean : 11.36 Mean :11.14 Mean :0.06917 Mean :0.5547 Mean :6.285 Mean : 68.57
##
## 3rd Qu.: 3.67708 3rd Qu.: 12.50 3rd Qu.:18.10 3rd Qu.:0.00000 3rd Qu.:0.6240 3rd Qu.:6.623 3rd Qu.: 94.08
##
## Max. :88.97620 Max. :100.00 Max. :27.74 Max. :1.00000 Max. :0.8710 Max. :8.780 Max. :100.00
## -----------------------------------------------------------------------------------------------------------------------
##
## Table: Summary of Boston data (continued below)
##
##
## ------------------------------------------------------------------------------------------------------------------
## dis rad tax ptratio black lstat medv
## ---------------- ---------------- --------------- --------------- ---------------- --------------- ---------------
## Min. : 1.130 Min. : 1.000 Min. :187.0 Min. :12.60 Min. : 0.32 Min. : 1.73 Min. : 5.00
##
## 1st Qu.: 2.100 1st Qu.: 4.000 1st Qu.:279.0 1st Qu.:17.40 1st Qu.:375.38 1st Qu.: 6.95 1st Qu.:17.02
##
## Median : 3.207 Median : 5.000 Median :330.0 Median :19.05 Median :391.44 Median :11.36 Median :21.20
##
## Mean : 3.795 Mean : 9.549 Mean :408.2 Mean :18.46 Mean :356.67 Mean :12.65 Mean :22.53
##
## 3rd Qu.: 5.188 3rd Qu.:24.000 3rd Qu.:666.0 3rd Qu.:20.20 3rd Qu.:396.23 3rd Qu.:16.95 3rd Qu.:25.00
##
## Max. :12.127 Max. :24.000 Max. :711.0 Max. :22.00 Max. :396.90 Max. :37.97 Max. :50.00
## ------------------------------------------------------------------------------------------------------------------
After getting a summary of the data, it’s worthwhile to get graphical representation. This time we will make a correlogram, a graphical representation of coorelation matrix. The corrplot function of corrplot package wll be used.
library(corrplot)
## corrplot 0.84 loaded
library(dplyr)
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:MASS':
##
## select
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
corr_boston<-cor(Boston) %>% round(2)
pandoc.table(corr_boston, split.table = 120)
##
## -------------------------------------------------------------------------------------------------------------------------------
## crim zn indus chas nox rm age dis rad tax ptratio black lstat medv
## ------------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- --------- ------- ------- -------
## **crim** 1 -0.2 0.41 -0.06 0.42 -0.22 0.35 -0.38 0.63 0.58 0.29 -0.39 0.46 -0.39
##
## **zn** -0.2 1 -0.53 -0.04 -0.52 0.31 -0.57 0.66 -0.31 -0.31 -0.39 0.18 -0.41 0.36
##
## **indus** 0.41 -0.53 1 0.06 0.76 -0.39 0.64 -0.71 0.6 0.72 0.38 -0.36 0.6 -0.48
##
## **chas** -0.06 -0.04 0.06 1 0.09 0.09 0.09 -0.1 -0.01 -0.04 -0.12 0.05 -0.05 0.18
##
## **nox** 0.42 -0.52 0.76 0.09 1 -0.3 0.73 -0.77 0.61 0.67 0.19 -0.38 0.59 -0.43
##
## **rm** -0.22 0.31 -0.39 0.09 -0.3 1 -0.24 0.21 -0.21 -0.29 -0.36 0.13 -0.61 0.7
##
## **age** 0.35 -0.57 0.64 0.09 0.73 -0.24 1 -0.75 0.46 0.51 0.26 -0.27 0.6 -0.38
##
## **dis** -0.38 0.66 -0.71 -0.1 -0.77 0.21 -0.75 1 -0.49 -0.53 -0.23 0.29 -0.5 0.25
##
## **rad** 0.63 -0.31 0.6 -0.01 0.61 -0.21 0.46 -0.49 1 0.91 0.46 -0.44 0.49 -0.38
##
## **tax** 0.58 -0.31 0.72 -0.04 0.67 -0.29 0.51 -0.53 0.91 1 0.46 -0.44 0.54 -0.47
##
## **ptratio** 0.29 -0.39 0.38 -0.12 0.19 -0.36 0.26 -0.23 0.46 0.46 1 -0.18 0.37 -0.51
##
## **black** -0.39 0.18 -0.36 0.05 -0.38 0.13 -0.27 0.29 -0.44 -0.44 -0.18 1 -0.37 0.33
##
## **lstat** 0.46 -0.41 0.6 -0.05 0.59 -0.61 0.6 -0.5 0.49 0.54 0.37 -0.37 1 -0.74
##
## **medv** -0.39 0.36 -0.48 0.18 -0.43 0.7 -0.38 0.25 -0.38 -0.47 -0.51 0.33 -0.74 1
## -------------------------------------------------------------------------------------------------------------------------------
corrplot(corr_boston, method = "circle", tl.col = "black", cl.pos="b", tl.pos = "d", type = "upper" , tl.cex = 0.9 )
In the graph, positive correlations are displayed in blue and negative correlations in red color with intensity of the color and circle size being proportional to the correlation coefficients.
Data Standardization
boston_scaled<-scale(Boston)
pandoc.table(summary(Boston), split.table = 120)
##
## -----------------------------------------------------------------------------------------------------------------------
## crim zn indus chas nox rm age
## ------------------ ---------------- --------------- ----------------- ---------------- --------------- ----------------
## Min. : 0.00632 Min. : 0.00 Min. : 0.46 Min. :0.00000 Min. :0.3850 Min. :3.561 Min. : 2.90
##
## 1st Qu.: 0.08204 1st Qu.: 0.00 1st Qu.: 5.19 1st Qu.:0.00000 1st Qu.:0.4490 1st Qu.:5.886 1st Qu.: 45.02
##
## Median : 0.25651 Median : 0.00 Median : 9.69 Median :0.00000 Median :0.5380 Median :6.208 Median : 77.50
##
## Mean : 3.61352 Mean : 11.36 Mean :11.14 Mean :0.06917 Mean :0.5547 Mean :6.285 Mean : 68.57
##
## 3rd Qu.: 3.67708 3rd Qu.: 12.50 3rd Qu.:18.10 3rd Qu.:0.00000 3rd Qu.:0.6240 3rd Qu.:6.623 3rd Qu.: 94.08
##
## Max. :88.97620 Max. :100.00 Max. :27.74 Max. :1.00000 Max. :0.8710 Max. :8.780 Max. :100.00
## -----------------------------------------------------------------------------------------------------------------------
##
## Table: Table continues below
##
##
## ------------------------------------------------------------------------------------------------------------------
## dis rad tax ptratio black lstat medv
## ---------------- ---------------- --------------- --------------- ---------------- --------------- ---------------
## Min. : 1.130 Min. : 1.000 Min. :187.0 Min. :12.60 Min. : 0.32 Min. : 1.73 Min. : 5.00
##
## 1st Qu.: 2.100 1st Qu.: 4.000 1st Qu.:279.0 1st Qu.:17.40 1st Qu.:375.38 1st Qu.: 6.95 1st Qu.:17.02
##
## Median : 3.207 Median : 5.000 Median :330.0 Median :19.05 Median :391.44 Median :11.36 Median :21.20
##
## Mean : 3.795 Mean : 9.549 Mean :408.2 Mean :18.46 Mean :356.67 Mean :12.65 Mean :22.53
##
## 3rd Qu.: 5.188 3rd Qu.:24.000 3rd Qu.:666.0 3rd Qu.:20.20 3rd Qu.:396.23 3rd Qu.:16.95 3rd Qu.:25.00
##
## Max. :12.127 Max. :24.000 Max. :711.0 Max. :22.00 Max. :396.90 Max. :37.97 Max. :50.00
## ------------------------------------------------------------------------------------------------------------------
Next, we will create quantile vector for crime
boston_scaled<- data.frame(boston_scaled)
qvc<-quantile(boston_scaled$crim)
crime <- cut(boston_scaled$crim, breaks = qvc, label = c("low", "med_low", "med_high", "high"), include.lowest = TRUE)
boston_scaled <- dplyr::select(boston_scaled, -crim)
boston_scaled<-data.frame(boston_scaled, crime)
table(boston_scaled$crime)
##
## low med_low med_high high
## 127 126 126 127
Linear Discriminant Analysis
library(MASS)
n<-nrow(boston_scaled)
ind <- sample(n, size = n*0.8)
train <- boston_scaled[ind,]
test <- boston_scaled[-ind,]
lda.fit <- lda(crime ~ ., data = train)
# target classes as numeric
classes <- as.numeric(train$crime)
# plot the lda results
plot(lda.fit, dimen = 2, col = classes, pch = classes)
Class Prediction
crime_cat<-test$crime
test<-dplyr::select(test, -crime)
lda.pred<-predict(lda.fit, newdata = test)
table(correct = crime_cat, predicted = lda.pred$class)
## predicted
## correct low med_low med_high high
## low 16 13 0 0
## med_low 3 19 7 0
## med_high 0 4 13 1
## high 0 0 0 26
K-means Clustering
data(Boston)
boston_scaled1<-as.data.frame(scale(Boston))
dist_eu<-dist(boston_scaled1)
summary(dist_eu)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.1343 3.4625 4.8241 4.9111 6.1863 14.3970
head(boston_scaled1)
## crim zn indus chas nox rm
## 1 -0.4193669 0.2845483 -1.2866362 -0.2723291 -0.1440749 0.4132629
## 2 -0.4169267 -0.4872402 -0.5927944 -0.2723291 -0.7395304 0.1940824
## 3 -0.4169290 -0.4872402 -0.5927944 -0.2723291 -0.7395304 1.2814456
## 4 -0.4163384 -0.4872402 -1.3055857 -0.2723291 -0.8344581 1.0152978
## 5 -0.4120741 -0.4872402 -1.3055857 -0.2723291 -0.8344581 1.2273620
## 6 -0.4166314 -0.4872402 -1.3055857 -0.2723291 -0.8344581 0.2068916
## age dis rad tax ptratio black
## 1 -0.1198948 0.140075 -0.9818712 -0.6659492 -1.4575580 0.4406159
## 2 0.3668034 0.556609 -0.8670245 -0.9863534 -0.3027945 0.4406159
## 3 -0.2655490 0.556609 -0.8670245 -0.9863534 -0.3027945 0.3960351
## 4 -0.8090878 1.076671 -0.7521778 -1.1050216 0.1129203 0.4157514
## 5 -0.5106743 1.076671 -0.7521778 -1.1050216 0.1129203 0.4406159
## 6 -0.3508100 1.076671 -0.7521778 -1.1050216 0.1129203 0.4101651
## lstat medv
## 1 -1.0744990 0.1595278
## 2 -0.4919525 -0.1014239
## 3 -1.2075324 1.3229375
## 4 -1.3601708 1.1815886
## 5 -1.0254866 1.4860323
## 6 -1.0422909 0.6705582
Inspired by this R-blogger post and this Stackoverflow question
First we start with random cluster number. Let’s start with k=4 and apply k-means on the data.
#Let us apply kmeans for k=4 clusters
kmm = kmeans(boston_scaled1,6,nstart = 50 ,iter.max = 15) #we keep number of iter.max=15 to ensure the algorithm converges and nstart=50 to ensure that atleat 50 random sets are choosen
Let’s use Elbow method to estimate number of clusters.
#Elbow Method for finding the optimal number of clusters
library(ggplot2)
set.seed(1234)
# Compute and plot wss for k = 2 to k = 15.
k.max <- 15
data <- boston_scaled1
wss <- sapply(1:k.max,
function(k){kmeans(data, k)$tot.withinss})
#wss
qplot(1:k.max, wss, geom = c("point", "line"), span = 0.2,
xlab="Number of clusters K",
ylab="Total within-clusters sum of squares")
## Warning: Ignoring unknown parameters: span
## Warning: Ignoring unknown parameters: span
library(NbClust)
nb <- NbClust(boston_scaled1, diss=NULL, distance = "euclidean",
min.nc=2, max.nc=5, method = "kmeans",
index = "all", alphaBeale = 0.1)
## *** : The Hubert index is a graphical method of determining the number of clusters.
## In the plot of Hubert index, we seek a significant knee that corresponds to a
## significant increase of the value of the measure i.e the significant peak in Hubert
## index second differences plot.
##
## *** : The D index is a graphical method of determining the number of clusters.
## In the plot of D index, we seek a significant knee (the significant peak in Dindex
## second differences plot) that corresponds to a significant increase of the value of
## the measure.
##
## *******************************************************************
## * Among all indices:
## * 12 proposed 2 as the best number of clusters
## * 6 proposed 3 as the best number of clusters
## * 3 proposed 4 as the best number of clusters
## * 3 proposed 5 as the best number of clusters
##
## ***** Conclusion *****
##
## * According to the majority rule, the best number of clusters is 2
##
##
## *******************************************************************
#hist(nb$Best.nc[1,], breaks = max(na.omit(nb$Best.nc[1,])))
Now, it’s much clearer that the data is described better with two clusters. With that, we run k-means algorithm again.
#Let us apply kmeans for k=4 clusters
km_final = kmeans(boston_scaled1, centers = 2) #we keep number of iter.max=15 to ensure the algorithm converges and nstart=50 to ensure that atleat 50 random sets are choosen
pairs(boston_scaled1[3:9], col=km_final$cluster)
More LDA
boston_scaled2<-as.data.frame(scale(Boston))
head(boston_scaled2)
## crim zn indus chas nox rm
## 1 -0.4193669 0.2845483 -1.2866362 -0.2723291 -0.1440749 0.4132629
## 2 -0.4169267 -0.4872402 -0.5927944 -0.2723291 -0.7395304 0.1940824
## 3 -0.4169290 -0.4872402 -0.5927944 -0.2723291 -0.7395304 1.2814456
## 4 -0.4163384 -0.4872402 -1.3055857 -0.2723291 -0.8344581 1.0152978
## 5 -0.4120741 -0.4872402 -1.3055857 -0.2723291 -0.8344581 1.2273620
## 6 -0.4166314 -0.4872402 -1.3055857 -0.2723291 -0.8344581 0.2068916
## age dis rad tax ptratio black
## 1 -0.1198948 0.140075 -0.9818712 -0.6659492 -1.4575580 0.4406159
## 2 0.3668034 0.556609 -0.8670245 -0.9863534 -0.3027945 0.4406159
## 3 -0.2655490 0.556609 -0.8670245 -0.9863534 -0.3027945 0.3960351
## 4 -0.8090878 1.076671 -0.7521778 -1.1050216 0.1129203 0.4157514
## 5 -0.5106743 1.076671 -0.7521778 -1.1050216 0.1129203 0.4406159
## 6 -0.3508100 1.076671 -0.7521778 -1.1050216 0.1129203 0.4101651
## lstat medv
## 1 -1.0744990 0.1595278
## 2 -0.4919525 -0.1014239
## 3 -1.2075324 1.3229375
## 4 -1.3601708 1.1815886
## 5 -1.0254866 1.4860323
## 6 -1.0422909 0.6705582
set.seed(1234)
km_bs2<-kmeans(dist_eu, centers = 6)
#head(km_bs2)
myclust<-data.frame(km_bs2$cluster)
boston_scaled2$clust<-km_bs2$cluster
#myclust<-data.frame(km_bs2$cluster)
#myvar$boston_scaled2
#myvar <- (myclust[,1])
head(boston_scaled2)
## crim zn indus chas nox rm
## 1 -0.4193669 0.2845483 -1.2866362 -0.2723291 -0.1440749 0.4132629
## 2 -0.4169267 -0.4872402 -0.5927944 -0.2723291 -0.7395304 0.1940824
## 3 -0.4169290 -0.4872402 -0.5927944 -0.2723291 -0.7395304 1.2814456
## 4 -0.4163384 -0.4872402 -1.3055857 -0.2723291 -0.8344581 1.0152978
## 5 -0.4120741 -0.4872402 -1.3055857 -0.2723291 -0.8344581 1.2273620
## 6 -0.4166314 -0.4872402 -1.3055857 -0.2723291 -0.8344581 0.2068916
## age dis rad tax ptratio black
## 1 -0.1198948 0.140075 -0.9818712 -0.6659492 -1.4575580 0.4406159
## 2 0.3668034 0.556609 -0.8670245 -0.9863534 -0.3027945 0.4406159
## 3 -0.2655490 0.556609 -0.8670245 -0.9863534 -0.3027945 0.3960351
## 4 -0.8090878 1.076671 -0.7521778 -1.1050216 0.1129203 0.4157514
## 5 -0.5106743 1.076671 -0.7521778 -1.1050216 0.1129203 0.4406159
## 6 -0.3508100 1.076671 -0.7521778 -1.1050216 0.1129203 0.4101651
## lstat medv clust
## 1 -1.0744990 0.1595278 6
## 2 -0.4919525 -0.1014239 6
## 3 -1.2075324 1.3229375 6
## 4 -1.3601708 1.1815886 6
## 5 -1.0254866 1.4860323 6
## 6 -1.0422909 0.6705582 6
lda.fit_bs2<-lda(clust~., data = boston_scaled2 )
lda.fit_bs2
## Call:
## lda(clust ~ ., data = boston_scaled2)
##
## Prior probabilities of groups:
## 1 2 3 4 5 6
## 0.10079051 0.19960474 0.09486166 0.20553360 0.12845850 0.27075099
##
## Group means:
## crim zn indus chas nox rm
## 1 -0.4149170 2.55535505 -1.228758914 -0.1951310 -1.21919439 0.78676843
## 2 0.3880377 -0.48724019 1.165421314 -0.2723291 0.98659851 -0.28553884
## 3 -0.3613809 -0.09419977 -0.474086929 1.5321752 -0.12487357 1.27068222
## 4 -0.3580718 -0.46023584 -0.003188584 -0.2723291 -0.09478548 -0.35414265
## 5 1.4172264 -0.48724019 1.069802298 0.4545202 1.34622349 -0.73713928
## 6 -0.4055840 0.02149547 -0.740804469 -0.2723291 -0.79649957 0.09099544
## age dis rad tax ptratio black
## 1 -1.4488239 1.7464736 -0.7048880 -0.5692695 -0.8353442 0.34924852
## 2 0.7651453 -0.7898745 1.1388129 1.2431405 0.6932747 0.04498348
## 3 0.2307707 -0.3386056 -0.4961654 -0.7220694 -1.1226766 0.32813467
## 4 0.4093998 -0.2612071 -0.5865335 -0.4342609 0.2608189 0.19191309
## 5 0.8557425 -0.9615698 1.2885597 1.2934457 0.4142248 -1.68787016
## 6 -0.8223904 0.7053125 -0.5694290 -0.7355910 -0.2013102 0.37698635
## lstat medv
## 1 -0.9773530 0.8760790
## 2 0.6734731 -0.5987824
## 3 -0.6138415 1.4407282
## 4 0.1508360 -0.2838601
## 5 1.1961180 -0.8078336
## 6 -0.5996059 0.2092896
##
## Coefficients of linear discriminants:
## LD1 LD2 LD3 LD4 LD5
## crim 0.04811996 -0.28556378 -0.55488255 0.49400398 0.05329096
## zn -0.13738829 -1.83004313 0.34546140 -0.26802062 -0.87758918
## indus 0.74925386 -0.10015651 0.61607026 -0.42031079 0.25109137
## chas 0.13287282 -0.13228082 -0.94523359 -0.16829634 0.04786106
## nox 1.21764057 -0.81216848 -0.12506389 0.27633410 0.13213424
## rm -0.12060003 -0.04058521 -0.02502279 -0.75468374 0.21331834
## age 0.17397462 0.34382124 -0.07430813 -0.37956005 -0.95205471
## dis -0.36273454 -0.54652248 0.11546588 0.26210162 0.59195828
## rad 0.61453519 0.40958433 0.29006265 -0.40963042 1.56473994
## tax 0.75124298 -1.03741454 0.22707980 -0.17126395 -0.61781814
## ptratio 0.36217649 -0.18603253 0.30060517 0.16017164 -0.53729844
## black -0.27542772 0.27016025 0.77143821 -0.87012879 0.23445845
## lstat 0.48988940 -0.40861927 -0.53017288 -0.23295699 -0.06758426
## medv 0.22977036 -0.57759705 -0.86635437 -0.06977308 -0.10361245
##
## Proportion of trace:
## LD1 LD2 LD3 LD4 LD5
## 0.7285 0.1498 0.0750 0.0298 0.0168
# the function for lda biplot arrows
lda.arrows <- function(x, myscale = 1, arrow_heads = 0.1, color = "red", tex = 0.75, choices = c(1,2)){
heads <- coef(x)
arrows(x0 = 0, y0 = 0,
x1 = myscale * heads[,choices[1]],
y1 = myscale * heads[,choices[2]], col=color, length = arrow_heads)
text(myscale * heads[,choices], labels = row.names(heads),
cex = tex, col=color, pos=3)
}
plot(lda.fit_bs2, dimen = 2)
lda.arrows(lda.fit_bs2, myscale = 3)
Better ways to visualize LDA
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:MASS':
##
## select
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
model_predictors <- dplyr::select(train, -crime)
# check the dimensions
dim(model_predictors)
## [1] 404 13
dim(lda.fit$scaling)
## [1] 13 3
# matrix multiplication
matrix_product <- as.matrix(model_predictors) %*% lda.fit$scaling
matrix_product <- as.data.frame(matrix_product)
plot_ly(x = matrix_product$LD1, y = matrix_product$LD2, z = matrix_product$LD3, type= 'scatter3d', mode='markers', color = train$crim)
#Second 3D plot where colors are defined by clusters of k-means
#k-means_matpro<-kmeans(matrix_product, )
#head(train)
#train$cl<-myclust
#boston_scaled2$cl<-myclust
#head(boston_scaled2)
#head(train)
#rownames(train)
#rownames(boston_scaled2)
train$cl <- boston_scaled2$clust[match(rownames(train), rownames(boston_scaled2))]
#head(train)
#nrow(train)
plot_ly(x = matrix_product$LD1, y = matrix_product$LD2, z = matrix_product$LD3, type = "scatter3d", mode="markers", color = train$cl)
Additional links (also included in the course slides)